package com.zw.yzk.kabu.view.fragment;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.adapter.MallItemListAdapter;
import com.zw.yzk.kabu.base.BaseFragment;
import com.zw.yzk.kabu.internal.component.DaggerMallComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.MallItemModule;
import com.zw.yzk.kabu.model.MallItemModel;
import com.zw.yzk.kabu.presenter.MallPresenter;
import com.zw.yzk.kabu.view.activity.GoodsClassifyActivity;
import com.zw.yzk.kabu.view.activity.MainActivity;
import com.zw.yzk.kabu.view.activity.ShoppingCartActivity;
import com.zw.yzk.kabu.view.view.MallView;
import com.zw.yzk.refresh.library.PullToRefreshLayout;
import com.zw.yzk.refresh.library.RefreshBuilder;
import com.zw.yzk.refresh.library.State;
import com.zw.yzk.refresh.library.refrsh.DefaultRefreshViewCreator;

import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2016/12/28.
 */

public class MallFragment extends BaseFragment implements MallView {

    @Bind(R.id.refresh_view)
    PullToRefreshLayout refreshLayout;
    @Bind(R.id.goods)
    RecyclerView goods;

    @Inject
    MallPresenter presenter;
    private MainActivity activity;
    private MallItemListAdapter adapter;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        activity = (MainActivity) context;
    }

    @Override
    protected int setContent() {
        return R.layout.fragment_mall;
    }

    @Override
    protected void initView(View view) {
        initRecyclerView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void loadData() {
        getGoods(0, true);
    }

    @Override
    public void initComponent() {
        DaggerMallComponent.builder()
                .applicationComponent(activity.getApplicationComponent())
                .activityModule(new ActivityModule(activity))
                .mallItemModule(new MallItemModule())
                .build()
                .inject(this);

        presenter.setView(this);
    }

    @OnClick(R.id.filter)
    public void filterGoods() {
        startActivity(new Intent(activity, GoodsClassifyActivity.class));
    }

    @Override
    public void getGoodsSuccess(List<MallItemModel> list) {
        if (refreshLayout.isLoading()) {
            adapter.add(list);
            refreshLayout.loadMoreFinish(list.isEmpty() ? State.LOAD_ALL : State.LOADING_SUCCEED);
        } else {
            adapter.refresh(list);
            refreshLayout.refreshFinish(State.REFRESH_SUCCEED);
        }
    }

    @OnClick(R.id.shopping_cart)
    public void showShoppingCart() {
        startActivity(new Intent(activity, ShoppingCartActivity.class));
    }

    @Override
    public void getGoodsFailed() {
        refreshLayout.finishRefreshingOrLoadingStatus(false);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView() {

        goods.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        adapter = new MallItemListAdapter(null);
        goods.setAdapter(adapter);
        goods.setItemAnimator(new DefaultItemAnimator());
        new RefreshBuilder.Builder(refreshLayout)
                .setRefreshViewCreator(new DefaultRefreshViewCreator())
                .setLoadMoreEnable(false)
                .setRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
                    @Override
                    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                        getGoods(0, false);
                    }

                    @Override
                    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                    }
                }).build();
    }


    /**
     * 获取商品列表
     *
     * @param start    开始索引
     * @param getLocal 是否获取本地数据
     */
    private void getGoods(int start, boolean getLocal) {
        presenter.getGoods(start, getLocal);
    }
}
